/*
This file is part of buco.  
Copyright (c) 2012 Christopher Pawelzik <christopher@cpawelzik.com>

Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php 
*/ 

function BundleWireView(wire, connection) { 
	// wire model object
	this.wire = wire; 		
	// jsplumb connection 
	this.connection = connection; 
}

BundleWireView.prototype.show = function(target) { 		
	$("#bundleInfo").empty(); 				
	buco.propertyLayout.hide("south"); 
	
	// Create required elements
	var treeview = $("<div id=\"treeview\"></div>"); 
	var wrap = $("<div></div>").addClass("bundleInfoWrap"); 	
	var fromBundle = buco.helper.getBundle(this.wire.from);
	var toBundle = buco.helper.getBundle(this.wire.to);
	$("#bundleInfo").append(wrap); 
	$("#bundleInfo").append(treeview); 	
	
	var text = fromBundle.bundleSymbolicName + " -> " + toBundle.bundleSymbolicName
	var wireFromTo = $("<p></p>").addClass("bundleName").text(text); 
	var activateButton = $("<button id='toggleWireButton'></button>"); 	
	activateButton.text(this.wire.isActive ? "Disable" : "Enable"); 
	
	$(wrap).append(wireFromTo); 
	$(wrap).append(activateButton); 
	
	$(activateButton).button(); 
	var fn = function(wire, connection) {
		$(activateButton).click(function() {
			buco.mvc.executeAction("composition.toggleWire", {wire: wire, connection: connection});	
		}); 
	}(this.wire, this.connection);
	
	// Transform bundle model to a jstree object		
	var imports = []; 
	var requiredBundles = []; 
	var exports = []; 	
	
	// Create imports and required bundles
	for(var i = 0; i < this.wire.requirements.length; i++) { 
		var req = this.wire.requirements[i]; 				
		if(req.type == "ImportPackageRequirement") {
			var text = req.packageName; 
			if(req.packageVersionRange != null) { 
				text += ", version=" + req.packageVersionRange; 
			}
			imports.push({data: text});			
		}
		else if(req.type = "RequireBundleRequirement") { 			
			var text = req.bundleSymbolicName; 
			if(req.bundleVersionRange != null) { 
				text += ", version=" + req.bundleVersionRange;
			}
			requiredBundles.push( {data: text} );			
		}		
	}
	
	// Create exports
	for(var i = 0; i < this.wire.capabilities.length; i++) { 
		var cap = this.wire.capabilities[i]; 		
		var node = {data: cap.packageName}
		if(cap.packageVersion != null) { 
			node.data += ", version=" + cap.packageVersion; 
		}
		exports.push(node);					
	}
			
	var nodes = []
	
	if(imports.length > 0) {
		nodes.push( {data: {title: "Imported Packages", attr: { "class" : "treeViewRoot" }}, children: imports, state: "open"} );
	}
	if(exports.length > 0) {
		nodes.push( {data: {title: "Exported Packages", attr: { "class" : "treeViewRoot" } }, children: exports, state: "open"} );
	}	
	if(requiredBundles.length > 0) { 		
		nodes.push( {data: {title: "Required Bundles", attr: { "class" : "treeViewRoot" }}, children: requiredBundles, state: "open"} ); 
	}
		
	// Render tree view 
	$(treeview).jstree({ 
		json_data: {"data" : nodes},
		plugins: ["themes", "json_data", "ui"],
		themes : {
			theme: "default", 
			dots: true, 
			icons: false
		}
	});
}
